// pages/index/change.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    coupons: '',
    id: '',
    count: '',
    goods_eval_list: [],
    showPopup: false,
    info: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(JSON.stringify({url: '_input_charset=\"utf-8\"&body=\"novel\"&it_b_pay=\"30m\"&notify_url=\"https://tp-pay-test.snssdk.com/callback/ali_pay\"&out_trade_no=\"201808211755020406852103\"&partner=\"2088801374045154\"&payment_type=\"1\"&seller_id=\"adsense@bytedance.com\"&service=\"mobile.securitypay.pay\"&subject=\"测试的商品\"&total_fee=\"0.01\"&sign=\"RGdwAoCy5DsjdFBdtrN9WzdYtyZGlUHn8dbAQVQsIPidLTR9s%2BCVtAj%2BtYzL8oAHP0IXJZw8U6EGlyA2MG2ZxhJRI1N1RhDMZOz56eAXO%2FITZYiGSB01hkhx9yhqmWAUJQfUMRHJZswS1DEpwam1JfaoahZ%2Bf%2FEE%2FkvG6ma67t4%3D\"&sign_type=\"RSA\"'}));
    if (options.id) {
      this.setData({
        id: options.id
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log('12312312');
    this.getInfo();
    var coupons = this.data.coupons;
    if(app.globalData.token){
      if(coupons == "" || coupons == 'undefined' || coupons == null){
        console.log('1');
        this.getCoupons();
      }
    }
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },

  /**
   * 获取试用兑换详情
   */
  getInfo: function () {
    var _this = this;
    tt.showLoading({
      title: '加载中...',
    })
    tt.request({
      url: app.globalData.url + 'index.php?act=toutiao_index&op=get_try_info', // 目标服务器url
      data: {
        id: _this.data.id
      },
      dataType: 'json',
      success: function (res) {
        if (res.data.code == 200) {
          tt.hideLoading();
          _this.setData({
            info: res.data.datas.info
          })
          _this.getComment();
        } else {
          tt.hideLoading();
          wx.showModal({
            title: '提示',
            content: '获取失败',
            showCancel: false,
            success: function(res) {
             
            }
          })
        }
      }
    });
  },
  /**
   * 获取试用兑换评论
   */
  getComment: function () {
    var _this = this;
    tt.request({
      url: app.globalData.url + 'index.php?act=toutiao_index&op=goods_evaluate', // 目标服务器url
      data: {
        goods_id: _this.data.id
      },
      dataType: 'json',
      success: function (res) {
        if (res.data.code == 200) {
          _this.setData({
            count: res.data.datas.count,
            goods_eval_list: res.data.datas.goods_eval_list
          })
        } else {
          wx.showModal({
            title: '提示',
            content: '获取失败',
            showCancel: false,
            success: function(res) {
             
            }
          })
        }
      }
    });
  },
  onChangeShowPopup: function () {
    var _this = this;
    if (!app.globalData.token) {
      if (app.globalData.is_login == 0) {
        tt.navigateTo({
          url: '/pages/my/login',
        });
      } else {
        tt.switchTab({
          url: '/pages/index/index',
        });
      }
    }else{
      var coupons = _this.data.coupons;
      if(coupons == "" || coupons == 'undefined' || coupons == null){
        _this.getCoupons();
      }
      var showPopup = _this.data.showPopup;
      _this.setData({
        showPopup: !showPopup
      })
    }
    
  },
  getCoupons: function () {
    var _this = this;
    tt.request({
      url: app.globalData.url + 'index.php?act=toutiao_member&op=get_member_coupon&key=' + encodeURI(app.globalData.token), // 目标服务器url
      dataType: 'json',
      success: function (res) {
        if (res.data.code == 200) {
          _this.setData({
            coupons: res.data.datas.coupons ? res.data.datas.coupons : '0'
          })
        }
      }
    });
  },
  sureChange: function () {
    var _this = this;
    tt.request({
      url: app.globalData.url + 'index.php?act=member_box&op=exchangeGift' + '&key=' + encodeURI(app.globalData.token), // 目标服务器url
      data: {
        pgoods_id: _this.data.id,
        is_toutiao: '1'
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      method: 'POST',
      dataType: 'json',
      success: function (res) {
        if (res.data.code == 200) {
          wx.showModal({
            title: '提示',
            content: res.data.datas,
            showCancel: false,
            success: function(res) {
              tt.navigateTo({
                url: '/pages/my/my'
              })
            }
          })
        } else {
          tt.hideLoading();
          wx.showModal({
            title: '提示',
            content: res.data.datas.error,
            showCancel: false,
            success: function(res) {
             
            }
          })
        }
      }
    });
  }
})